﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LambdaAnonimousExtensionMethods
{
    public static class Extensions
    {
        public static StringBuilder SubStrinBuilder(this StringBuilder str, int index, int length)
        {
            StringBuilder myBuilder = new StringBuilder();
            for (int i = index; i < index + length; i++)
            {
                myBuilder.Append(str[i]);
            }
            return myBuilder;
            //string result = str.ToString();
            //result = result.Substring(index, length);
            //return new StringBuilder(result);
        }

        public static decimal SumElements<T>(this IEnumerable<T> elements)
        {
            decimal sum = 0;
            foreach (var item in elements)
            {
                sum += Convert.ToDecimal(item);
            }
            return sum;
        }

        public static decimal ProductElements<T>(this IEnumerable<T> elements)
        {
            decimal product = 1;
            foreach (var item in elements)
            {
                product *= Convert.ToDecimal(item);
            }
            return product;
        }

        public static decimal AvarageElements<T>(this IEnumerable<T> elements)
        {
            decimal product = 0;
            foreach (var item in elements)
            {
                product += Convert.ToDecimal(item);
            }
            return product/elements.Count<T>();
        }

        public static T MinElements<T>(this IEnumerable<T> elements) where T:IComparable<T>
        {
            T minElement=elements.First<T>();
            foreach (var item in elements)
            {
                if (minElement.CompareTo(item)>0)
                {
                    minElement = item;
                }
            }
            return minElement;
        }

        public static T MaxElements<T>(this IEnumerable<T> elements) where T : IComparable<T>
        {
            T maxElement = elements.First<T>();
            foreach (var item in elements)
            {
                if (maxElement.CompareTo(item) > 0)
                {
                    maxElement = item;
                }
            }
            return maxElement;
        }
    }
}
